#pragma once
#include <QMenu>
#include "interfaces/GuiPlugin.h"
#include "StarlabMainWindow.h"

#ifdef DEBUG_PLUGIN_VIEWCONTROL
    #define DEB 
#else
    #define DEB if(false)
#endif

class gui_view : public GuiPlugin{
    Q_OBJECT
    Q_INTERFACES(GuiPlugin)
    
private:
    StarlabMainWindow* window;
public:

    void load(StarlabMainWindow* window){
        this->window = window;
        load_selectdrawarea();
        // load_viewfrom();
        // load_fullscreen();
        // load_copypasteviewmatrix();
        
        { /// @todo
            // toolBarMenu = viewMenu->addMenu(tr("&ToolBars"));
            // toolBarMenu->addAction(showToolbarStandardAct);
            // connect(toolBarMenu,SIGNAL(aboutToShow()),this,SLOT(updateMenus()));
        }
    }
    
    void load_copypasteviewmatrix(){
        QMenu* menu = window->viewMenu->addMenu("View Matrix");
        {
            QAction* action = new QAction (tr("Copy View Matrix"), this);
            action->setShortcut(QKeySequence::Copy);
            connect(action, SIGNAL(triggered()), window->drawArea(), SLOT(viewToClipboard()));
            menu->addAction(action);
        }
        {
            QAction* action = new QAction (tr("Paste View Matrix"), this);
            action->setShortcut(QKeySequence::Paste);
            connect(action, SIGNAL(triggered()), window->drawArea(), SLOT(viewFromClipboard()));
            menu->addAction(action);
        }
    }

    void load_selectdrawarea(){
        QMenu* menu = window->viewMenu->addMenu("DrawArea");
        QActionGroup* group =  new QActionGroup(this);
        group->setExclusive(true);
        foreach(DrawAreaPlugin* plugin, window->pluginManager.drawAreaPlugins)
            menu->addAction( new QAction(plugin->name(),group) );
        connect(group, SIGNAL(triggered(QAction *)), this, SLOT(selectDrawArea(QAction *)));
        menu->setEnabled(true);
    }
        
    void load_fullscreen(){
        QAction* action = new QAction (tr("&Toggle FullScreen"), this);
        action->setCheckable(true);
        action->setShortcutContext(Qt::ApplicationShortcut);
        action->setShortcut(Qt::ALT+Qt::Key_Return);
        connect(action, SIGNAL(triggered(bool)), window, SLOT(toggle_fullscreen(bool)));            
        window->viewMenu->addAction(action);
    }

    void load_viewfrom(){
        QMenu* viewFromMenu = window->viewMenu->addMenu("View From");

        QAction* reset = new QAction (tr("Reset"), this);
        reset->setShortcutContext(Qt::ApplicationShortcut);
        reset->setShortcut(Qt::CTRL+Qt::SHIFT+Qt::Key_H);
        connect(reset, SIGNAL(triggered()), window->drawArea(), SLOT(viewFrom()));
        viewFromMenu->addAction(reset);
    
        QActionGroup* group =  new QActionGroup(this);
        group->setExclusive(true);
        viewFromMenu->addAction( new QAction(tr("Top"),group) );
        viewFromMenu->addAction( new QAction(tr("Bottom"),group) );
        viewFromMenu->addAction( new QAction(tr("Left"),group) );
        viewFromMenu->addAction( new QAction(tr("Right"),group) );
        viewFromMenu->addAction( new QAction(tr("Front"),group) );
        viewFromMenu->addAction( new QAction(tr("Back"),group) );
        connect(group, SIGNAL(triggered(QAction *)), window->drawArea(), SLOT(viewFrom(QAction *)));

        viewFromMenu->setEnabled(true);
    }
    
    
private slots:   
    /// Loads the drawArea associated with the action
    void selectDrawArea(QAction* action){
        Q_ASSERT(action!=NULL);
        QString pluginName = action->text();
        DrawAreaPlugin* drawAreaPlugin = window->pluginManager.drawAreaPlugins.value(pluginName,NULL);
        if(!drawAreaPlugin) throw StarlabException("Requested drawArea not found"); 
        StarlabDrawArea* drawArea = drawAreaPlugin->load(window, &(window->document),&(window->settings));
        window->setupDrawArea(drawArea);
        
        /// Store in the setting as default value
        window->settings.set("DefaultDrawAreaPlugin",pluginName);
    }
};

//void MainWindow::fullScreen() {
//    if(!isFullScreen()) {
//        toolbarState = saveState();
//        menuBar()->hide();
//        mainToolBar->hide();
//        globalStatusBar()->hide();
//        setWindowState(windowState()^Qt::WindowFullScreen);
//    } else {
//        menuBar()->show();
//        restoreState(toolbarState);
//        globalStatusBar()->show();
//        setWindowState(windowState()^ Qt::WindowFullScreen);
//        fullScreenAct->setChecked(false);
//    }
//}
